﻿using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.IO;
using System.Net;
using System.Security.Policy;
using System.Text;
using IterationLife.Datas;
using IterationLife.Domains.Sports.Enums;
using IterationLife.Domains.Sports.Models;
using IterationLife.Domains.Sports.Queries;
using IterationLife.Domains.Sports.Repositories;
using IterationLife.Domains.Sports.Services;
using IterationLife.Extensions.HttpRequest;
using IterationLife.Football.MTSports.Configs;
using IterationLife.Football.MTSports.Contracts.Sports;
using IterationLife.Football.MTSports.Dtos.Sports;
using Util.Applications;
using Util.Datas.Queries;
using Util.Exceptions;

namespace IterationLife.Football.MTSports.Impl.Sports
{
    /// <summary>
    /// SportUsers服务
    /// </summary>
    public class SportUserService : ApplicationServiceBase<SportUser, SportUserDto, SportUserQuery>, ISportUserService
    {
        /// <summary>
        /// 初始化SportUsers服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="sportUserRepository">SportUsers仓储</param>
        public SportUserService(IApplicationUnitOfWork unitOfWork, ISportUserRepository sportUserRepository, ILoginManager loginManager)
            : base(unitOfWork, sportUserRepository)
        {
            SportUserRepository = sportUserRepository;
            LoginManager = loginManager;
        }

        /// <summary>
        /// SportUsers仓储
        /// </summary>
        protected ISportUserRepository SportUserRepository { get; set; }

        /// <summary>
        /// 登录管理
        /// </summary>
        protected ILoginManager LoginManager { get; set; }

        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override SportUserDto ToDto(SportUser entity)
        {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override SportUser ToEntity(SportUserDto dto)
        {
            return dto.ToEntity();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">SportUsers查询实体</param>
        protected override IQueryBase<SportUser> CreateQuery(SportUserQuery param)
        {
            return new Query<SportUser>(param);
        }

        /// <summary>
        /// 登录
        /// </summary>
        public SportUserDto Login(string userName, string password)
        {
            var result = LoginManager.Login(userName, password, EquipmentType.MT).ToDto();
            UnitOfWork.Commit();
            return result;
        }
    }
}